import request from '@/utils/request'

// 资源配额类型定义
export interface ResourceQuota {
  id: string
  tenantId: string
  cpu: number
  memory: number
  storage: number
  bandwidth: number
  maxUsers: number
  maxProjects: number
  createdAt: string
  updatedAt: string
}

// 租户类型定义
export interface Tenant {
  id: string
  tenantName: string
  tenantCode: string
  status: 'active' | 'inactive' | 'suspended'
  adminId: string
  adminName: string
  description?: string
  quotaId: string
  quota?: ResourceQuota
  createdAt: string
  updatedAt: string
}

// 租户查询参数
export interface TenantQuery {
  page?: number
  pageSize?: number
  keyword?: string
  status?: Tenant['status']
  tenantCode?: string
}

// 创建租户
export const createTenant = (
  tenantData: Omit<Tenant, 'id' | 'createdAt' | 'updatedAt' | 'adminName' | 'quota'> & {
    quota: Omit<ResourceQuota, 'id' | 'tenantId' | 'createdAt' | 'updatedAt'>
  },
) => {
  return request.post<Tenant>('/tenants', tenantData)
}

// 获取租户列表
export const getTenants = (query?: TenantQuery) => {
  return request.get<{ list: Tenant[]; total: number }>('/tenants', { params: query })
}

// 获取单个租户
export const getTenant = (id: string) => {
  return request.get<Tenant>(`/tenants/${id}`)
}

// 更新租户
export const updateTenant = (
  id: string,
  tenantData: Partial<Omit<Tenant, 'id' | 'createdAt' | 'updatedAt' | 'adminName' | 'quota'>>,
) => {
  return request.put<Tenant>(`/tenants/${id}`, tenantData)
}

// 删除租户
export const deleteTenant = (id: string) => {
  return request.delete(`/tenants/${id}`)
}

// 批量删除租户
export const batchDeleteTenants = (ids: string[]) => {
  return request.delete('/tenants/batch', { data: { ids } })
}

// 启用租户
export const enableTenant = (id: string) => {
  return request.post<Tenant>(`/tenants/${id}/enable`)
}

// 禁用租户
export const disableTenant = (id: string) => {
  return request.post<Tenant>(`/tenants/${id}/disable`)
}

// 暂停租户
export const suspendTenant = (id: string) => {
  return request.post<Tenant>(`/tenants/${id}/suspend`)
}

// 获取租户资源配额
export const getTenantQuota = (tenantId: string) => {
  return request.get<ResourceQuota>(`/tenants/${tenantId}/quota`)
}

// 更新租户资源配额
export const updateTenantQuota = (
  tenantId: string,
  quotaData: Omit<ResourceQuota, 'id' | 'tenantId' | 'createdAt' | 'updatedAt'>,
) => {
  return request.put<ResourceQuota>(`/tenants/${tenantId}/quota`, quotaData)
}

// 获取租户使用统计
export const getTenantUsage = (tenantId: string) => {
  return request.get<{
    cpu: { used: number; total: number }
    memory: { used: number; total: number }
    storage: { used: number; total: number }
    bandwidth: { used: number; total: number }
    users: { used: number; total: number }
    projects: { used: number; total: number }
  }>(`/tenants/${tenantId}/usage`)
}

// 获取所有租户使用统计
export const getAllTenantsUsage = () => {
  return request.get<
    Array<{
      tenantId: string
      tenantName: string
      cpu: { used: number; total: number }
      memory: { used: number; total: number }
      storage: { used: number; total: number }
    }>
  >('/tenants/usage')
}
